def 
  i   : Int;
  cps : Int;
  target_rate : Float;
  fine_tune_factor : Float;
  PileUp : Float;

  SampleDetectionWL : Float;
  SamplePosition    : Int;
  IRFDetectionWL    : Float;
  ScatterPosition   : Int;
  PeakCounts        : Int;

  StartTemp : Float;
  StopTemp  : Float;
  TempStep  : Float;
  TargetTemp: Float;
  EqDelay   : Int;

exec
{ Please adapt the following values to your needs }
{ ----------------------------------------------- }
  Sample="mySample";
  Solvent="mySolvent";

  SamplePosition=1;      { position of sample in the sample changer }
  SampleDetectionWL=510; { Detection wavelength given in nanometer }

  ScatterPosition=4;     { position of scatter solution in the sample changer }
  IRFDetectionWL=482;    { Laser IRF wavelength given in nanometer }

  PeakCounts = 10000;
  PileUp=0.01;

  StartTemp=10; { given in C }
  StopTemp=40;  
  TempStep=5;

  EqDelay=60;    { delay in seconds to wait for temp. equilibrium in the sample }

{ You should not need to edit the script below this line }
{ ------------------------------------------------------ }
  DETECTION_POLARIZER.ACTPos=54.7;
  for TargetTemp=StartTemp to StopTemp step TempStep
    SAMPLE_CHANGER.SMPTargetTemp=TargetTemp;
    SAMPLE_CHANGER.SMPTempCtrlEnabled=TRUE;
    DETECTOR_1.DETShutterPos=FALSE;
    EXCITATION_ATTENUATOR.ACTPos=1; { close excitation shutter }
    SAMPLE_CHANGER.SMPPos=SamplePosition; { go to sample to be measured }
    DETECTION_MONO.MCRGratingWavelength=SampleDetectionWL*1E-9;
    DETECTION_ATTENUATOR.ACTPos=10;
    DisplayStatus("Wait to reach "+TargetTemp+" C");
    Delay(3);
   { values of SAMPLE_CHANGER.SMPTempCtrlState
      disabled = 0
      stabilized = 1
      heating = 2
      cooling = 3
    }
    while(not(SAMPLE_CHANGER.SMPTempCtrlState==1))
      Delay(1);
    end;
    DisplayStatus("Wait "+EqDelay+" seconds to reach temp. equilibrium in the sample");
    Delay(EqDelay);
    { temperature is stable }
  

  { Begin of simple count rate optimizer for sample data acquisition }

    DisplayStatus("Optimize count rate for sample");
    target_rate=Sepia2.RepRate*PileUp;
    EXCITATION_ATTENUATOR.ACTPos=2;
    DETECTION_ATTENUATOR.ACTPos=10;
    DETECTOR_1.DETShutterPos=TRUE;

  { get the current count rate }
    Histogram(0.5, -1);
    cps=0;
    for i = 0 to crv[LastMeasCurveIndex].NumPoints-1 step 1
      cps=cps+crv[LastMeasCurveIndex].y[i];
    end;
    DeleteMeasCurve(LastMeasCurveIndex);
    cps=cps*2;
  { current count rate is available in variable cps }
  
    while((cps<(target_rate/10.0))and(EXCITATION_ATTENUATOR.ACTPos<5))
      EXCITATION_ATTENUATOR.ACTPos=EXCITATION_ATTENUATOR.ACTPos+1;

    { get the current count rate }
      Histogram(0.5, -1);
      cps=0;
      for i = 0 to crv[LastMeasCurveIndex].NumPoints-1 step 1
        cps=cps+crv[LastMeasCurveIndex].y[i];
      end;
      DeleteMeasCurve(LastMeasCurveIndex);
      cps=cps*2;
    { current count rate is available in variable cps }
    end;
  
    fine_tune_factor=target_rate/cps;
    if(fine_tune_factor>10) then
      fine_tune_factor=10.0;
    end;

    DETECTION_ATTENUATOR.ACTPos=10*fine_tune_factor;

  { End of simple count rate optimizer }

    DisplayStatus("Measure sample decay at "+TargetTemp+" C");
    Histogram(-1,PeakCounts);

    DisplayStatus("Move to scatter sample and laser wavelength");
    DETECTOR_1.DETShutterPos=FALSE;
    target_rate=Sepia2.RepRate*PileUp*0.1;
    SAMPLE_CHANGER.SMPPos=ScatterPosition; { go to scatter solution for IRF acquisition }
    DETECTION_MONO.MCRGratingWavelength=IRFDetectionWL*1E-9;

  { Begin of simple count rate optimizer for IRF acquisition }

    EXCITATION_ATTENUATOR.ACTPos=2;
    DETECTION_ATTENUATOR.ACTPos=10;
    DETECTOR_1.DETShutterPos=TRUE;

    DisplayStatus("Optimize count rate for scatter sample");
  { get the current count rate }
    Histogram(0.5, -1);
    cps=0;
    for i = 0 to crv[LastMeasCurveIndex].NumPoints-1 step 1
      cps=cps+crv[LastMeasCurveIndex].y[i];
    end;
    DeleteMeasCurve(LastMeasCurveIndex);
    cps=cps*2;
  { current count rate is available in variable cps }
  
    while((cps<(target_rate/10.0))and(EXCITATION_ATTENUATOR.ACTPos<5))

      EXCITATION_ATTENUATOR.ACTPos=EXCITATION_ATTENUATOR.ACTPos+1;

      { get the current count rate }
      Histogram(0.5, -1);
      cps=0;
      for i = 0 to crv[LastMeasCurveIndex].NumPoints-1 step 1
        cps=cps+crv[LastMeasCurveIndex].y[i];
      end;
      DeleteMeasCurve(LastMeasCurveIndex);
      cps=cps*2;
    { current count rate is available in variable cps }
    end;
  
    fine_tune_factor=target_rate/cps;
    if(fine_tune_factor>10) then
      fine_tune_factor=10.0;
    end;

    DETECTION_ATTENUATOR.ACTPos=10*fine_tune_factor;

  { End of simple count rate optimizer }

    DisplayStatus("Measure IRF histogram");
    Histogram(-1,PeakCounts);

 end; {end of temperature loop }

 DETECTOR_1.DETShutterPos=FALSE;
 EXCITATION_ATTENUATOR.ACTPos=1;

end.